﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  DispatcherHelpers.cs
//    
// Abstract:
//
//  This module implements some commonly used helper methods for Microsoft Enhanced Reminders Dispatcher.
//  
//--

using System;
using System.IO;
using System.Globalization;

namespace Microsoft.EnhancedReminders.Dispatcher
{
    /// <summary>
    /// This module implements some commonly used helper methods for Microsoft Enhanced Reminders Dispatcher.
    /// </summary>
    public static class DispatcherHelpers
    {
        internal const string SCRATCH_DIRECTORY = "DispatcherScratch";

        /// <summary>
        /// Returns the path to the scratch directory to use for temporarily storing voicemail reminders
        /// </summary>
        public static string ScratchDirectory
        {
            get
            {
                return Path.Combine(Path.GetTempPath(), SCRATCH_DIRECTORY);
            }
        }

        /// <summary>
        /// Obtains a string representing the lead time before an appointment or meeting.
        /// </summary>
        /// <param name="cultureInfo">The local culture for the recipient of the reminder</param>
        /// <param name="timeZone">The local time zone for the recipient of the reminder</param>
        /// <param name="startTime">The time the calendar event begins in UTC</param>
        /// <param name="formatter">A ITimespanFormatter for doing the actual formatting of lengths of time</param>
        /// <returns>A string representing the timespan between the delivery of a reminder and the start time of a calendar item in local time</returns>
        public static string GetLeadTime(CultureInfo cultureInfo, TimeZoneInfo timeZone, DateTime startTime, ITimeSpanFormatter formatter)
        {
            DateTime now = DateTime.UtcNow;
            DateTime localStartTime = TimeZoneInfo.ConvertTimeFromUtc(startTime, timeZone);
            DateTime localNow = TimeZoneInfo.ConvertTimeFromUtc(now, timeZone);
            TimeSpan span = startTime.Subtract(now);

            if (span.TotalHours < 1)
            {
                if (span.Minutes <= 0)
                {
                    return formatter.OverdueTimeSpan(cultureInfo, localStartTime);
                }
                else
                {
                    return formatter.ImmediateTimeSpan(cultureInfo, span);
                }
            }
            else
            {
                if (span.TotalHours <= 24.0 && localStartTime.Day == localNow.Day)
                {
                    return formatter.TimeSpanToday(cultureInfo, localStartTime);
                }
                // If the time span between the reminder and now is less than 48 hours and is the next calendar day, it is tomorrow
                else if (span.TotalHours < 48.0 && ((Math.Abs(localNow.DayOfWeek - localStartTime.DayOfWeek) % 7) == 1 || (Math.Abs(localNow.DayOfWeek - localStartTime.DayOfWeek) % 7) == 6))
                {
                    return formatter.TimeSpanTomorrow(cultureInfo, localStartTime);
                }
                else
                {
                    return formatter.DistantTimeSpan(cultureInfo, localStartTime);
                }
            }
        }
    }
}
